package com.pingan.haofang.searchcloud.dashboard.service;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;

import com.pingan.haofang.searchcloud.common.constants.Constants;
import com.pingan.haofang.searchcloud.dashboard.dto.BasicMailDto;
import com.pingan.haofang.searchcloud.dashboard.dto.BasicMonitorDto;
import com.pingan.haofang.searchcloud.dashboard.dto.IndexDatasourceGroupDto;
import com.pingan.haofang.searchcloud.index.constants.IndexDatasourceStatus;
import com.pingan.haofang.searchcloud.indexdatasource.dao.IndexDatasourceDao;
import com.pingan.haofang.searchcloud.mail.dto.MailDto;
import com.pingan.haofang.searchcloud.mail.service.MailService;
import com.pingan.haofang.searchcloud.user.dao.SearchProjectDao;
import com.pingan.haofang.searchcloud.user.domain.SearchProject;

/**
 * 监控基础类
 *
 * @author EX-SUNBIXIAO001
 * @date 2018/4/19
 * @since 1.0.0
 */
public class BasicMonitorService {

    @Autowired
    protected SearchProjectDao searchProjectDao;

    @Autowired
    protected IndexDatasourceDao indexDatasourceDao;

    @Autowired
    protected MailService mailService;

    /**
     * 获得ProjectMap
     * @return
     */
    protected Map<Long, SearchProject> getProjectMap(){
        List<SearchProject> projectList = searchProjectDao.findAll();
        return projectList.stream().collect(Collectors
                .toMap(SearchProject::getId, java.util.function.Function.identity(), (o, n) -> n));
    }

    /**
     * 获得DatasourceNumMap
     * @return
     */
    protected Map<Long, Long> getDatasourceNumMap(){
        List<IndexDatasourceGroupDto> indexDatasources = indexDatasourceDao.findByStatus(IndexDatasourceStatus.NORMAL.getId());
        return indexDatasources.stream().collect(Collectors
                .toMap(IndexDatasourceGroupDto::getIndexId, IndexDatasourceGroupDto::getNum, (o, n) -> n));

    }


    /**
     * 填充项目名称和数据源数量
     *
     * @return
     */
    protected void buildBasicMonitorDto(List<? extends BasicMonitorDto> dtos,Map<Long, SearchProject>projectMap, Map<Long, Long> datasourceNumMap ) {

        dtos.stream().forEach( s -> {
            s.setProjectName(Optional.ofNullable(projectMap.get(s.getProjectId())).map(SearchProject::getProjectName).orElse(""));
            s.setDatasourceNum(Optional.ofNullable(datasourceNumMap.get(s.getIndexId())).map(Long::intValue).orElse(0));

        });

    }


    /**
     * 發送提醒郵件
     *
     * @param monitorDto
     */
    public void sendNotify(BasicMailDto monitorDto) {

        MailDto<BasicMailDto> mailDto = new MailDto<>();
        mailDto.setTemplateName(monitorDto.getMailTemplate());
        mailDto.setEmailTo(monitorDto.getMailAddress());
        mailDto.setSubject(String.format(Constants.REPORT_TEMPLATE_MAIL_TITLE, monitorDto.getEnv(), monitorDto.getTitle(), monitorDto.getReportDate()));
        mailDto.setModel(monitorDto);
        mailService.sendMail(mailDto);
    }
}
